home *** CD-ROM | disk | FTP | other *** search
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Newsgroups: comp.lang.c
- Subject: Re: A Linked List Problem
- Date: 22 Feb 1996 19:14:30 GMT
- Organization: Ripco Communications, Inc.
- Message-ID: <4gifam$2ut@gail.ripco.com>
- NNTP-Posting-Host: golden.ripco.com
-
- SHAHZAD ANJUM MALIK <X60CC@CUNYVM.CUNY.EDU> in
- <96053.015114X60CC@CUNYVM.CUNY.EDU> asks:
-
-
- > The problem is that the complier (MSC 6.00) gives syntax error on the
- > 2nd last line. I have been trying to remove the mistake for several
- > days but useless. The error for "current->Mag = current->Mag.nextMag"
- > is " Erro No. '=' incompatible types...
-
- In the revision of your code below, there is a comment regarding this
- (correct) diagnostic.
-
- There are several other errors and some obfuscation in your code, some
- of which have been fixed. The changes that I have made are marked with
- comments beginning with `/* mha -'.
-
- The addition of explicit return and return type to main() are stylistic,
- as is the change in the malloc() invocation. I have avoided other
- stylistic changes, but these are well worth incorporating into your
- general coding style and may save you from errors you might otherwise
- never find. You should get the FAQ from rtfm.mit.edu and read why you
- should avoid gets() and how you can do this.
-
- [X60CC's original code with revisions noted with `/* mha -']
-
- #include <stdlib.h> /* mha - added */
- #include <stdio.h> /* mha - added */
- /* mha - You may have #included these in your original code, but they
- * are missing here and the (unneeded) cast of malloc() in your code
- * suggests that stdlib.h has not been included */
-
- struct CustName {
- char LastName[15];
- char IDnum[5]; /* mha - was `char ID#[5];'. `#' is not
- * a legal constituent character in a
- * C identifier. */
- };
- struct Magazine {
- char MagName[20];
- struct Magazine *nextMag;
- };
-
- struct CustProfile {
- struct CustName Name;
- struct Magazine Mag;
- struct CustProfile *next;
- };
- typedef struct CustProfile ELEMENT;
- typedef ELEMENT *LINK;
-
-
- int main()
- { /* mha - added explicit return type */
- LINK current, head;
- LINK NewNode; /* mha - added. You _must_ declare
- * variables before use. */
- head = NULL;
- NewNode = malloc(sizeof *NewNode);
- /* mha - was `NewNode = (LINK) malloc(sizeof(ELEMENT));' */
-
- if (head == NULL)
- head = current = NewNode;
- else {
- current = head;
- while (current->next != NULL) /* mha - was `! =' */
- current = current->next;
- current->next = NewNode;
- current = NewNode;
- }
-
- printf("Enter Customer Name: ");
- gets(current->Name.LastName);
- printf("Enter Mag Name");
- gets(current->Mag.MagName);
- printf("Enter Second Mag Name");
- gets(current->Mag.nextMag->MagName);
-
- current->Mag = *(current->Mag.nextMag);
- /* mha - was `current->Mag = current->Mag.nextMag;'.
- * `current->Mag' is a `struct Magazine', while
- * `current->Mag.nextMag' is `struct Magazine *'. I don't know
- * what you intend to be doing, but the replacement is at least
- * legal. */
-
- current->next = NULL;
- return 0; /* mha - added explicit return */
- }
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-